import requests_html
from openpyxl import load_workbook
from tqdm import tqdm
import  csv
sourceFile='TestData_TopSearch_SearchKeywordHotData.xlsx'
wb=load_workbook(sourceFile)
sheet=wb['Sheet1']
sourceRows=sheet.max_row
sourceData=[]
for index in (range(sourceRows)):
    sourceData.append(sheet.cell(index+1,1).value)

sourceData=sourceData[1:]

#二维数组，为了能多行写入csv
toCsvList=[]
session = requests_html.HTMLSession()

for index_source,source in enumerate(tqdm(sourceData)):
    onedata=[]
    url = 'https://www.newegg.com/p/pl?d='+str(source).replace(' ','+')
    web = session.get(url=url)
    htmlObj = web.html
    atc=htmlObj.xpath('//*[ @ id = "app"] / div[1] / div[1] / ol / li[2] / text()', first=True)
    if atc!=None and 'Search Results:' in atc:
        onedata.append(source)
    if onedata!=[]:
        toCsvList.append(onedata)

with open(sourceFile[:-4]+'csv','w',newline='')as file:
    op=csv.writer(file)
    op.writerows(toCsvList)



